<?php
declare (strict_types = 1);

namespace app\command\elastic;

use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;
use think\facade\Log;
use app\model\shop\Order;
use app\model\vipshop\VipOrder;
use app\elastic\OrderElastic;

class OrderInit extends Command
{
    protected $orderElastic;

    protected function configure()
    {
        // 指令配置
        $this->setName('init:order')
             ->setDescription('ES数据初始化:订单');
    }

    protected function execute(Input $input, Output $output)
    {
        $this->orderElastic = new OrderElastic();

        //判断索引名是否存在 不存在创建索引 存在修改索引
        if (!$this->orderElastic->exists()){
            Log::info('索引不存在，准备创建');
            $this->orderElastic->createIndex();
            Log::info('创建成功，准备初始化数据');
            $this->rebuildShopData();
            $this->rebuildVipShopData();
            Log::info('操作成功');
        }
    }

    protected function rebuildShopData()
    {
        Log::info('准备同步严选商城数据:');
        $order = Order::field('
           order_id,order_no,out_trade_no,order_type,payment_type,shipping_type,order_status,
           pay_status,buyer_id,user_name,buyer_message,create_time,pay_time,receiver_mobile
           receiver_province,receiver_city,receiver_district,receiver_address,receiver_name,
           goods_money,order_money,shipping_money,pay_money
        ')
        ->append(['doc_id','data_source'])
        ->with(['order_item' => function($query){
            $query->field('
                order_goods_id,order_id,goods_id,goods_name,sku_id,sku_name,price,
                adjust_money,goods_money,goods_picture,goods_type,order_status,shipping_status,
                refund_status, refund_time,order_real_money
            ');
        }])
        ->chunk(100, function ($orders){
            if(!$orders->count()){
                Log::error('同步失败');
                exit('同步失败');
            }
            Log::info(sprintf('正在同步 ID 范围为 %s 至 %s 的订单', $orders->first()->order_id, $orders->last()->order_id));
            $this->orderElastic->bulkDoc($orders->toArray());
            Log::info(sprintf('ID 范围为 %s 至 %s 的订单同步成功', $orders->first()->order_id, $orders->last()->order_id));
        });
        Log::info('同步严选商城数据结束');
    }

    protected function rebuildVipShopData()
    {
        Log::info('准备同步VIP商城数据:');

        $a = VipOrder::field('
            order_id,order_no,out_trade_no,order_type,payment_type,shipping_type,order_status,
            pay_status,buyer_id,user_name,buyer_message,create_time,pay_time,receiver_mobile
            receiver_province,receiver_city,receiver_district,receiver_address,receiver_name,
            goods_money,order_money,shipping_money,pay_money
        ')
        ->append(['doc_id','data_source'])
        ->with(['order_item' => function($query){
            $query->field('
                order_goods_id,order_id,goods_id,goods_name,sku_id,sku_name,price,
                adjust_money,goods_money,goods_picture,goods_type,order_status,shipping_status,
                refund_status, refund_time,order_real_money
            ');
        }])
        ->chunk(100, function ($orders){
            if(!$orders->count()){
                Log::error('同步失败');
                exit('同步失败');
            }
            Log::info(sprintf('正在同步 ID 范围为 %s 至 %s 的订单', $orders->first()->order_id, $orders->last()->order_id));
            $this->orderElastic->bulkDoc($orders->toArray());
            Log::info(sprintf('ID 范围为 %s 至 %s 的订单同步成功', $orders->first()->order_id, $orders->last()->order_id));
        });
        Log::info('同步VIP商城数据结束');
    }
}
